import { getGoodsInfo, addOrUpdateShopCart } from '@/api/search.js'
import { getToken } from '@/utils/token'
const state = {
  goodInfo: {},
  uuid_token: getToken()
}
const mutations = {
  SET_GOOD_INFO (state, payload) {
    state.goodInfo = payload
  }
}
const actions = {
  async getGoodInfo ({ commit }, id) {
    const res = await getGoodsInfo(id)
    commit('SET_GOOD_INFO', res.data)
  },
  async addOrUpdateShopCart ({ commit }, { skuId, skuNum }) {
    const res = await addOrUpdateShopCart(skuId, skuNum)
    // console.log('res:', res)
    if (res.code === 200) {
      return 'ok'
    } else {
      return Promise.reject(new Error('fail'))
    }
  }
}
const getters = {
  categoryView (state) {
    return state.goodInfo.categoryView || {}
  },
  skuInfo (state) {
    return state.goodInfo.skuInfo || {}
  },
  spuSaleAttrList (state) {
    return state.goodInfo.spuSaleAttrList || {}
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
